package com.xiyuan.view;

import com.xiyuan.PathDirector;
import com.xiyuan.bean.Result;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Main {

    private JFrame frame;
    private JTextField className;//类名字段
    private JButton createDoc;//生成文档按钮
    private JTextPane textArea;//生成结果

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main() {
        initialize();
        initListener();

    }


    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1223, 636);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(22, 26, 146, 533);
        frame.getContentPane().add(panel);

        JButton btnNewButton = new JButton("规则配置");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        JButton btnNewButton_3 = new JButton("首页");
        btnNewButton_3.setFont(new Font("宋体", Font.PLAIN, 15));
        panel.add(btnNewButton_3);
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
        panel.add(btnNewButton);

        JLabel lblNewLabel = new JLabel("全限定类名");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel.setBounds(178, 26, 85, 56);
        frame.getContentPane().add(lblNewLabel);

        className = new JTextField();
        className.setFont(new Font("宋体", Font.PLAIN, 15));
        className.setText("com.xiyuan.controller.ExecutiveVideoPublishingController");
        className.setToolTipText("输入类名路径");
        className.setBounds(273, 37, 361, 28);
        frame.getContentPane().add(className);
        className.setColumns(10);


        createDoc = new JButton("生成接口文档");
        createDoc.setFont(new Font("宋体", Font.PLAIN, 17));
        createDoc.setToolTipText("生成接口文档");
        createDoc.setBounds(501, 517, 206, 47);
        frame.getContentPane().add(createDoc);


        JLabel lblNewLabel_1 = new JLabel("生成结果");
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));
        lblNewLabel_1.setBounds(176, 110, 85, 56);
        frame.getContentPane().add(lblNewLabel_1);
        
        JPanel panel_1 = new JPanel();
        panel_1.setBounds(284, 136, 343, 361);
        frame.getContentPane().add(panel_1);
        panel_1.setLayout(null);


        textArea = new JTextPane();
        textArea.setBounds(9, 5, 327, 348);
        panel_1.add(textArea);
        
        JScrollPane scrollBar = new JScrollPane();
        scrollBar.setBounds(220, 92, 0, 43);
        panel_1.add(scrollBar);


  
    }

    /**
     * 初始化监听器
     */
    public void initListener() {
        createDoc.addActionListener(e -> {
            try {
                final Result result = createDoc();
                textArea.setText(result.getJsonString());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
    }

    /**
     * 生成接口文档
     *
     * @return
     * @throws Exception
     */
    public Result createDoc() throws Exception {
        String text = className.getText();
        Class<?> aClass = Class.forName(text);
        if (text != null && text != "") {
            Result build = new PathDirector().builder(aClass).build();
            if (build == null) {
                throw new ClassNotFoundException("请输入正确的全限定类名");
            }
            return build;
        }
        throw new Exception("解析失败");
    }
}
